<template>
  <div class="home">
    <transition name="fade">
      <router-view></router-view>
    </transition>
    <van-tabbar v-model="active" fixed @change="changeClick">
      <van-tabbar-item :to="HomePage" active-class='active'>
        <span>首页</span>
        <img
          slot="icon"
          slot-scope="props"
          :src="props.active ? homeIcon.active : homeIcon.normal"
        >
      </van-tabbar-item>
      <van-tabbar-item to="/user" active-class='active'>
        <span>我的</span>
        <img
          slot="icon"
          slot-scope="props"
          :src="props.active ? myIcon.active : myIcon.normal"
        >
      </van-tabbar-item>
    </van-tabbar>

  <van-popup v-model="PopupShow" :close-on-click-overlay='true'>
    <img class="money-ico" src="../../assets/money1.png" alt="">
    <p class="hint">太棒了，钱已经到账啦！</p>
    <div class="steps">
      <div class="dot"><i class="first"></i><i class="last"></i></div>
      <div class="firstText">
        <p class="h-Tit">放款成功</p>
        <div class="h-hint"><span>恭喜您放款成功</span><span>{{loanTime}}</span></div>
      </div>
      <div class="lastText">
        <p class="h-Tit">申请提现</p>
        <div class="h-hint"><span>申请金额：{{loanMoney}}元</span><span>{{contractTime}}</span></div>
      </div>
    </div>
    <p class="repay">记得按时还款哦，逾期会影响人行征信</p>
    <div class="iKonw" @click="iKonwClick">我知道了</div>
  </van-popup>
  </div>
</template>

<script>
var axios = require("axios");
import { Tabbar, TabbarItem, Dialog, Toast, Popup,} from 'vant';

export default {
  components: {
    [Tabbar.name]:Tabbar,
    [TabbarItem.name]:TabbarItem,
    [Dialog.name]:Dialog,
    [Toast.name]:Toast,
    [Popup.name]:Popup,

  },
  data() {
    return {
      active: 0,
      token:'',
      homeIcon: {
        normal: require("../../assets/icon-home-normal.png"),
        active: require('../../assets/icon-home-active.png')
      },
      myIcon: {
        normal: require('../../assets/icon-my-normal.png'),
        active: require('../../assets/icon-my-active.png')
      },
      info:'',//用户信息
      HomePage:'/HomeBefore',//页面跳转
      PopupShow:false,
      latitude:'',//坐标
      longitude:'',//坐标
      contractTime:'',//申请时间
      loanTime:'',//放款时间
      loanMoney:''

    }
  },
  mounted(){
    let _this = this;
    //获取坐标
    let userAgentObj = navigator.userAgent;
    if (userAgentObj.match(/Android/i)) {
      window.control.getLocation();//坐标
    } else if (userAgentObj.match(/iPhone/i)) {

    }

    window.getLocation=(latitude,longitude)=>{
      let token = _this.token;
      if(latitude){
        // alert('1'+latitude)
        // alert('2'+longitude)
        this.deviceInfoStatus(token,latitude,longitude)
      }else{
        // 121.481674 31.234576
        let latitude = '';
        let longitude = '';
        this.deviceInfoStatus(token,latitude,longitude)
      }
    }

    let url= window.location.href;
    // 判断当前页面是 “首页”还是“我的”
    // console.log(url.indexOf('user'))
    if(url.indexOf('user')!=-1){
      this.active=1;
    }else{
      if(url.indexOf('HomeBefore')!=-1){
        this.HomePage = '/HomeBefore'
      }else if(url.indexOf('HomeIn')!=-1){
        this.HomePage = '/HomeIn'
      }else if(url.indexOf('HomeAfter')!=-1){
        this.HomePage = '/HomeAfter'
      }
    }
    // 获取本地数据
    this.$util.getUser().then(token=>{
      // console.log(token)
       this.token=token;
      if(token!='-1'){

        _this.userInfo(token);
        // _this.Version(token);
      }else{
        this.HomePage = '/HomeBefore'
        // this.$router.push({ path: '/HomeBefore'})
      }
    })
    Toast.loading({duration:1000,mask: false,message: '加载中...'});
  },
  methods:{
    changeClick(){
      let _this = this;
      console.log(123)
      this.$api.post("/user/v1/isLogin",{},s => {

      },f => {
        if(f.resultCode=='10000'||f.resultCode=='10003'||f.resultCode=='10001'||f.resultCode=='10002'){
          _this.HomePage = '/HomeBefore'
        }
      },
      {
        Accept: "*/*",
        "Content-Type": "application/json",
        'channelCode':this.$util.channelCode,
        'version':this.$util.version,
        'token':this.token
      })
    },
    // 是否需要上传设备信息

    deviceInfoStatus(token,latitude,longitude){
      let _this = this;

      this.$api.get('/sys/v1/deviceInfoStatus?deviceNo='+this.$util.deviceNo,{},s=>{
      // this.$api.get('/sys/v1/deviceInfoStatus?deviceNo='+'a5112d1212201e17',{},s=>{
        console.log(s)
        // alert(s.module)
        if(s.module==0){
          _this.deviceInfoUploads(token,latitude,latitude);
        }
      },f=>{
        Toast(f.errorMessage)
      },{
        'Accept': 'application/json',
        'channelCode':this.$util.channelCode,
        'version':this.$util.version,
        'token':this.token
      })
    },
    // 上传设备信息
    deviceInfoUploads(token,latitude,longitude){
      let _this = this;
      this.$util.deviceInfoUpload(token,latitude,longitude,function(s){
        console.log(s)
        // _this.$util.setSaveVlues('deviceInfoUpload','true')
      },function(f){
        console.log(f)
        // Toast(f.errorMessage);
      })
    },

    // 用户信息
    userInfo(token){
      let _this = this;
      let url = window.location.href;
      console.log(url)
      this.$api.post('/user/v1/info',{},s=>{
        _this.info = s.module;
        // console.log(_this.$util);
        _this.$util.info =  s.module;
        if(url.indexOf('user')!=-1){
          if(s.module.loanSchedules[0].state == 10){
            _this.HomePage = '/HomeBefore'
          }else if(s.module.loanSchedules[0].state == 20){
            _this.HomePage = '/HomeIn'
          }else if(s.module.loanSchedules[0].state == 30){
            _this.HomePage = '/HomeAfter'
          }
          return false;
        }
        // s.module.loanSchedules[0].state = 10
        if(s.module.loanSchedules[0].state == 10){

          if(_this.HomePage!='/HomeBefore'){
            // _this.$router.push({ path: '/HomeBefore'})
            _this.HomePage = '/HomeBefore'
            _this.$util.url('/HomeBefore','首页')

          }
        }else if(s.module.loanSchedules[0].state == 20){
          _this.$util.setSaveVlues("PopupShow", 'false');
          if(_this.HomePage!='/HomeIn'){
            // _this.$router.push({ path: '/HomeIn'})
             _this.HomePage = '/HomeIn'
            _this.$util.url('/HomeIn','首页')
          }
        }else if(s.module.loanSchedules[0].state == 30){
            if(_this.$util.getSaveVlues('PopupShow')=='false'){
              _this.borrowAfterSchedule();
              setTimeout(function(){
                _this.PopupShow=true;
              },500)


            }
            if(_this.HomePage!='/HomeAfter'){
              // _this.$router.push({ path: '/HomeAfter'})
              _this.HomePage = '/HomeAfter'
              _this.$util.url('/HomeAfter','首页')
            }

        }
      },f=>{
        if(f.resultCode=='10000'||f.resultCode=='10003'||f.resultCode=='10001'||f.resultCode=='10002'){
          if(_this.$util.getSaveVlues('token')!='-1'){
             Dialog.confirm({
              title: "温馨提示",
              message: "您的登录已过期，请重新登录！"
            })
              .then(() => {
                this.$util.url('/login','登录')
                // window.control.onBacks();
              })
              .catch(() => {
                console.log("返回");
                // on cancel
              });
          }
        }else{
          Toast(f.errorMessage)
        }
      },{
        'Accept': 'application/json',
        'channelCode':this.$util.channelCode,
        'version':this.$util.version,
        'token':token
      })
    },
    iKonwClick(){
      this.PopupShow= false;
      this.$util.setSaveVlues('PopupShow','true')
    },
    borrowAfterSchedule(){
       let _this = this;

      this.$api.get('/borrowAfter/v1/borrowAfterSchedule?bankrollCode='+this.$util.bankrollCode,{},s=>{
        // 申请时间
        _this.contractTime = _this.format(s.module.contractTime);
        // 放款时间
        _this.loanTime = _this.format(s.module.loanTime);
        _this.loanMoney = s.module.loanMoney/100;


      },f=>{
        if(f.resultCode=='10000'||f.resultCode=='10003'||f.resultCode=='10001'||f.resultCode=='10002'){
          _this.$util.url('/HomeBefore','首页')
        }else{
          Toast(f.errorMessage);
        }
      },{
        'Accept': 'application/json',
        'channelCode':this.$util.channelCode,
        'version':this.$util.version,
        'token':this.token
      })
    },
    add0(m){return m<10?'0'+m:m },
    format(shijianchuo){
      var time = new Date(shijianchuo);
      var y = time.getFullYear();
      var m = time.getMonth()+1;
      var d = time.getDate();
      var h = time.getHours();
      var mm = time.getMinutes();
      var s = time.getSeconds();
      return y+'-'+this.add0(m)+'-'+this.add0(d)+' '+this.add0(h)+':'+this.add0(mm)+':'+this.add0(s);
    }

  }
}
</script>


<style scoped >
  /***
  * 产品主题颜色需求
  * #FF8024 按钮 突出文字 ico
  * #FFD500 B辅助色
  * #FFEC9C c辅助色
  * #1D0F00 重要文字
  * #41424E 副标题段落
  * #797A79 次要文字
  * #A9AAA9 不可点击
  * #E6E6E6 分割线 描边
  * #F6F7F6 背景
  * #6699CC 链接
  */
.van-tabbar-item--active {
    color: #FF8024;
}
.home{
  width:100%;
  height:100%;
}
.van-popup{
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  width:309px;
  height:408px;
  border-radius: 10px;
  /* background:#ccc; */
}
.money-ico{
  width:121px;
  height:69px;
}
.hint{
  margin-top:15px;
  font-size:18px;
  line-height: 25px;
  color:#333;
}
.steps{
  width:249px;
  height:94px;
  padding: 0 0 0 20px;
  margin-top:30px;
  /* border:1px solid red; */
}
.repay{
  margin-top:30px;
  font-size:12px;
  line-height: 17px;
  color:#333;
}
.iKonw{
  width:249px;
  height:48px;
  margin-top:20px;
  font-size:16px;
  line-height: 48px;
  border-radius:48px;
  background: #FF8024;
  color:#fff;
}
.dot{
  position: absolute;
  display:flex;
  flex-direction: column;
  justify-content: flex-start;
  left:7px;
  width:1px;
  height:60px;
  margin-top:10px;
  border-left:1px solid rgba(255,128,36,0.7);
}
.dot .first{
  position:relative;
  left:-4px;
  display: inline-block;
  width:7px;
  height:7px;
  border-radius: 10px;
  background: #ff8024;
}
.dot .last{
  position:absolute;
  bottom:0;
  left:-4px;
  display: inline-block;
  width:7px;
  height:7px;
  border:1px solid #ccc;
  border-radius: 10px;
  background: #f5f5f5;
}
.firstText,.lastText{
  text-align: left;
  font-size:12px;
}
.firstText .h-Tit{
  font-size:14px;
  color:#333;
}
.firstText .h-Tit{
  margin-top:5px;
  font-size:14px;
  line-height:20px;
  color:#333;
}
.firstText .h-hint{
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  font-size:12px;
  line-height:22px;
  color:#333;
}
.lastText .h-Tit{
  margin-top:10px;
  font-size:14px;
  color:#999;
}
.lastText .h-hint{
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  font-size:12px;
  line-height:22px;
  color:#999;
}
</style>